package common_phrase

import (
	common_phrase_db "hft/Database/admin/common_phrase"
	"hft/Http/Models/request"
	"hft/Http/Services/common_phrase"
	utils "hft/Http/Utils"
	"hft/e"
	"strconv"

	"github.com/gin-gonic/gin"
)

type CommonPhrase struct{}

func (*CommonPhrase) List(c *gin.Context) {
	var category_id, _ = strconv.Atoi(c.Query("category_id"))
	if category_id == 0 {
		utils.FailWithMessage(e.CommonPhraseCateNotFound, c)
	} else {
		list := common_phrase.CommonPhraseService.List(category_id)
		utils.OkWithData(list, c)
	}
}

func (*CommonPhrase) Add(c *gin.Context) {
	var data *common_phrase_db.CommonPhrase
	c.ShouldBindJSON(&data)
	if err := common_phrase.CommonPhraseService.Add(data); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}
}

func (*CommonPhrase) Edit(c *gin.Context) {
	var data *common_phrase_db.CommonPhrase
	c.ShouldBindJSON(&data)
	if err := common_phrase.CommonPhraseService.Edit(data); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}
}

func (*CommonPhrase) Delete(c *gin.Context) {
	var ids *request.IDS
	c.ShouldBindJSON(&ids)
	if err := common_phrase.CommonPhraseService.Delete(ids.IDS); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}
}

var CommonPhraseController = new(CommonPhrase)
